package com.cn.xiaonuo.common.utils;

import com.google.gson.Gson;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

public class QiniuUtil {

    //access_key
    private static final String ACCESS_KEY="WJdWFlPbSjmekkhW47M-DxCyBZgM3FuWGTsM_1zS";

    //secret_key
    private static final String SECRET_KEY="NfrSTSWqng80tul0YnnQ6BfrrCsxtSKLhVQElkua";

//    //bucket
    private static final String BUCKET="gy";//新空间

    //bucket
//    private static final String BUCKET="zjc1518";//老空间（作废）

    // 密钥
    private static final Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);

//    //callbackurl
//    private static final String CALLBACKURL = "http://omhpz0ifi.bkt.clouddn.com/";//作废原来的地址，为了兼容小程序，必须使用https

    //callbackurl
    private static final String CALLBACKURL = "https://image.soole.com.cn/";//新地址

    public static String getUpToken(){
        return auth.uploadToken(BUCKET);
    }

    //普通上传文件并返回url访问地址(文件地址)
    public static String uploadFile(String filePath) throws Exception{
        try {
            //构造一个带指定Zone对象的配置类
            Configuration cfg = new Configuration(Zone.zone0());
            UploadManager uploadManager = new UploadManager(cfg);
            UUID uuid = UUID.randomUUID();
            String key = uuid.toString();
            String upToken=getUpToken();
            Response response = uploadManager.put(filePath, key, upToken);
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            return CALLBACKURL+putRet.key;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    //普通上传文件并返回url访问地址(文件地址)
    public static String uploadFile(String filePath,String fileName) throws Exception{
        try {
            //构造一个带指定Zone对象的配置类
            Configuration cfg = new Configuration(Zone.zone0());
            UploadManager uploadManager = new UploadManager(cfg);
            String key = fileName;
            String upToken=getUpToken();
            Response response = uploadManager.put(filePath, key, upToken);
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            return CALLBACKURL+putRet.key;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    //普通上传文件并返回url访问地址(文件对象)
    public static String uploadFile(File file) throws Exception{
        try {
            //构造一个带指定Zone对象的配置类
            Configuration cfg = new Configuration(Zone.zone0());
            UploadManager uploadManager = new UploadManager(cfg);
            UUID uuid = UUID.randomUUID();
            String fileName = file.getName();
            String prefix=fileName.substring(fileName.lastIndexOf("."));
            String key = uuid.toString()+prefix;

            String upToken=getUpToken();

            Response response = uploadManager.put(file, key, upToken);
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            return CALLBACKURL+putRet.key;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String uploadFile(MultipartFile multipartFile) throws  Exception{
        // 获取文件名
        String fileName = multipartFile.getOriginalFilename();
        // 获取文件后缀
        String prefix=fileName.substring(fileName.lastIndexOf("."));
        // 用uuid作为文件名，防止生成的临时文件重复
        File excelFile = File.createTempFile(UUID.randomUUID().toString(), prefix);
        // MultipartFile to File
        multipartFile.transferTo(excelFile);
        String url=uploadFile(excelFile);
        //删除临时文件
        excelFile.delete();
        return url;
    }

    public static String uploadFileData(MultipartFile multipartFile) throws  Exception{
        // 获取文件名
        String fileName = multipartFile.getOriginalFilename();
        // 获取文件后缀
        String prefix=fileName.substring(fileName.lastIndexOf("."));
        // 用uuid作为文件名，防止生成的临时文件重复
        File excelFile = File.createTempFile(UUID.randomUUID().toString(), prefix);
        // MultipartFile to File
        multipartFile.transferTo(excelFile);
        String url=uploadFileData(excelFile,prefix);
        //删除临时文件
        excelFile.delete();
        return url;
    }

    //普通上传文件并返回url访问地址(文件对象)
    public static String uploadFileData(File file,String prefix) throws Exception{
        try {
            //构造一个带指定Zone对象的配置类
            Configuration cfg = new Configuration(Zone.zone0());
            UploadManager uploadManager = new UploadManager(cfg);
            UUID uuid = UUID.randomUUID();
            String key = uuid.toString()+prefix;
            String upToken=getUpToken();
            Response response = uploadManager.put(file, key, upToken);
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            return CALLBACKURL+putRet.key;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static void main(String[] args) {
        try {
            File file = new File("C:\\\\Users\\\\Administrator\\\\Desktop\\\\upload");
            File[] files = file.listFiles();
            for(File file1:files){
                String file1Path=file1.getAbsolutePath();
                String fileName = file1.getName();
                System.err.println(file1Path+"  "+fileName);
                String s = uploadFile(file1Path, fileName);
                System.err.println(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
